import requests
from lxml import etree
import json

headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36'
}

def getpage(page):
    url = 'https://bj.58.com/zufang/pn%d/'%page
    res = requests.get(url,headers=headers)
    content = res.text
    parser = etree.HTML(content)
    house_list = parser.xpath("//ul[@class='house-list']/li")
    with open('58.json','w',encoding='utf-8') as fp:
        for house in house_list:
            title = house.xpath(".//h2/a/text()")
            if title:
                title = title[0].strip()
            room = house.xpath(".//p[@class='room']/text()")[0]
            if room:
                room = room[0].split(' ')[0]
            price = house.xpath(".//div[@class='money']//text()")
            if price:
                price = ''.join(price).strip()
            thumbnail = house.xpath(".//div[@class='img-list']//img/@lazy_src")
            if thumbnail:
                thumbnail = thumbnail[0]
            fp.write(json.dumps({'title':title,'room':room,'price':price},ensure_ascii=False)+'\n')

           

if __name__ == '__main__':
    for x in range(10):
        getpage(x)
        